package com.cb.myzhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.myzhxy.pojo.Teacher;
import com.cb.myzhxy.service.TeacherService;
import com.cb.myzhxy.util.MD5;
import com.cb.myzhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "教师信息管理控制器")
@RestController
@RequestMapping("/sms/teacherController")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 获取教师信息,分页带条件
     */
    @ApiOperation("获取教师信息,分页带条件")
    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    public Result getTeachers(@ApiParam("分页查询页码数") @PathVariable(value = "pageNo") Integer pageNo,
                              @ApiParam("分页查询页大小") @PathVariable(value = "pageSize") Integer pageSize,
                              @ApiParam("分页查询模糊匹配班级名")Teacher teacher){
        Page<Teacher> page = new Page<>(pageNo, pageSize);
        IPage<Teacher> page1=teacherService.getTeachersByOpr(page,teacher);
        return Result.ok(page1);
    }

    /**
     * 添加和修改教师信息
     */
    @ApiOperation("添加和修改教师信息")
    @PostMapping("/saveOrUpdateTeacher")
    public Result saveOrUpdateTeacher(@RequestBody Teacher teacher){
        if(null==teacher.getId()||0==teacher.getId()){
            teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        }
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    /**
     * 删除一个或者多个教师信息
     */
    @ApiOperation("删除一个或者多个教师信息")
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(@RequestBody List<Integer> ids){
        teacherService.removeByIds(ids);
        return Result.ok();
    }
}
